package com.jams.goodox.adapter;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lvman.configs.instrumentation.InstrumentedDraweeView;
import com.jams.goodox.R;
import com.jams.goodox.entity.AnnexBean;
import com.jams.goodox.entity.FastCarOrder;
import com.jams.goodox.listen.InItemViewClick;
import com.jams.library.fresco.FrescoUtil;
import com.jams.library.image.ImagePagerActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import uama.hangzhou.image.widget.MyGridView;

/**
 * 滴滴车主or乘客订单
 * Created by jams_zhen on 2017/5/26.
 */

public class CarOrderAdapter extends BaseQuickAdapter<FastCarOrder> {

    private InItemViewClick inItemViewClick;

    public CarOrderAdapter(List<FastCarOrder> fastCarOrderList) {
        super(R.layout.car_order_item, fastCarOrderList);
    }

    public void setInItemViewClick(InItemViewClick inItemViewClick) {
        this.inItemViewClick = inItemViewClick;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, FastCarOrder item) {
        baseViewHolder.setVisible(R.id.tx_whit_accept, item.isWhitAcceptFare());

        baseViewHolder.setVisible(R.id.relative_people, !item.isWhitAcceptFare());//带头像的顶部是否显示
        InstrumentedDraweeView instrumentedDraweeView = baseViewHolder.getView(R.id.img_icon);//设置头像
        FrescoUtil.loadImg(instrumentedDraweeView, item.getUserImg());
        baseViewHolder.setImageResource(R.id.img_sex, item.isMan() ? R.mipmap.male_icon : R.mipmap.female_icon);//性别
        baseViewHolder.setText(R.id.tx_name, item.getUserName());
        baseViewHolder.setVisible(R.id.tx_car_num, item.isCarNumShow());
        baseViewHolder.setText(R.id.tx_car_num, item.getCarNum());
        baseViewHolder.setVisible(R.id.tx_cancel, item.isCancelShow() || item.isWhitAcceptFare());

        baseViewHolder.setText(R.id.tx_data_time, item.getDateTime());
        baseViewHolder.setText(R.id.tx_people_num, item.getPeopleNumber());
        baseViewHolder.setText(R.id.tx_start_place, item.getStartPlace());
        baseViewHolder.setText(R.id.tx_end_place, item.getEndPlace());

        baseViewHolder.setText(R.id.tx_company, item.getEnterpriseName());
        baseViewHolder.setVisible(R.id.tx_company, !item.isFareOrder());

        baseViewHolder.setVisible(R.id.linear_pay_item, !item.isFareOrder());
        TextView tx_car_pay = baseViewHolder.getView(R.id.tx_car_pay);
        tx_car_pay.setCompoundDrawablesWithIntrinsicBounds(item.isPayMoney() ? R.mipmap.car_red : R.mipmap.car_gift, 0, 0, 0);
        tx_car_pay.setText(item.isPayMoney() ? item.getPayMoney() + "元" : item.getGoodName());
        MyGridView blankClickGridView = baseViewHolder.getView(R.id.gv_innings_item);
        blankClickGridView.setVisibility(item.getAnnexBeanList().size() == 0 ? View.GONE : View.VISIBLE);
        setImageList(blankClickGridView, item.getAnnexBeanList());

        TextView tx_order_status = baseViewHolder.getView(R.id.tx_order_status);
        tx_order_status.setVisibility(item.isWhitAcceptFare() ? View.GONE : View.VISIBLE);//是否显示订单状态栏
        switch (item.getOrderStatus()) {
            case FastCarOrder.Order_UnComplete:
                if (item.isFareOrder()) {//乘客订单，按钮显示：未完成，显示确认上车
                    tx_order_status.setText("确认上车");
                    tx_order_status.setBackgroundResource(R.drawable.bg_cancel_theme);
                    tx_order_status.setTextColor(ContextCompat.getColor(mContext, R.color.font_color_black));
                } else {
                    tx_order_status.setText("您有未完成的订单");
                    tx_order_status.setBackgroundResource(R.color.white);
                    tx_order_status.setTextColor(ContextCompat.getColor(mContext, R.color.font_color_red));
                }
                break;
            case FastCarOrder.Order_Complete:
                tx_order_status.setText("您的订单已完成");
                tx_order_status.setBackgroundResource(R.color.white);
                tx_order_status.setTextColor(ContextCompat.getColor(mContext, R.color.font_color_gray));
                break;
            case FastCarOrder.Order_Cancel:
                tx_order_status.setText("您的订单已取消");
                tx_order_status.setBackgroundResource(R.color.white);
                tx_order_status.setTextColor(ContextCompat.getColor(mContext, R.color.font_color_gray));
                break;
        }
        MyOnClick myOnClick = new MyOnClick(item);
        //设置点击事件
        baseViewHolder.setOnClickListener(R.id.tx_cancel, myOnClick);
        baseViewHolder.setOnClickListener(R.id.img_phone, myOnClick);
        baseViewHolder.setOnClickListener(R.id.img_sms, myOnClick);
        baseViewHolder.setOnClickListener(R.id.tx_order_status, myOnClick);
    }

    //设置图片
    private void setImageList(MyGridView gridView, final List<AnnexBean> annexBeanList) {
        PictureGroupAdapter gridAdapter = new PictureGroupAdapter(mContext, annexBeanList, 54, 9);
        gridView.setAdapter(gridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<String> imageListNew = new ArrayList<>();
                for (int i = 0; i < annexBeanList.size(); i++) {
                    imageListNew.add(annexBeanList.get(i).getUrlPath());
                }
                if (imageListNew.size() == 4 && position > 1) {
                    position--;
                }
                Intent intent = new Intent(mContext, ImagePagerActivity.class);
                intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, (Serializable) imageListNew);
                mContext.startActivity(intent);
            }
        });
    }

    private class MyOnClick implements View.OnClickListener {

        private FastCarOrder fastCarOrder;

        public MyOnClick(FastCarOrder fastCarOrder) {
            this.fastCarOrder = fastCarOrder;
        }

        @Override
        public void onClick(View v) {
            if (inItemViewClick == null) return;
            switch (v.getId()) {
                case R.id.tx_cancel:
                    if (fastCarOrder.isFareOrder() && !FastCarOrder.Order_Complete.equalsIgnoreCase(fastCarOrder.getOrderStatus()))
                        inItemViewClick.onViewClick(fastCarOrder, 1);
                    break;
                case R.id.img_phone:
                    inItemViewClick.onViewClick(fastCarOrder, 2);
                    break;
                case R.id.img_sms:
                    inItemViewClick.onViewClick(fastCarOrder, 3);
                    break;
                case R.id.tx_order_status:
                    if (!fastCarOrder.isCancelShow()) return;
                    inItemViewClick.onViewClick(fastCarOrder, 4);
                    break;
            }
        }
    }
}
